<template>
    <el-row class="lives-news" id="articleList">
        <el-row>
            <el-col class="title">
                <h3>{{displayName}}</h3>
            </el-col>
        </el-row>

        <el-row>
            <el-form :inline="true" class="inline-form">
                <el-form-item>
                    <el-button @click="addConfig" type="primary"><i class="el-icon-edit el-icon--left"></i>修改</el-button>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-col>
                <p>
                    <template v-if="type == '文字'">{{value}}</template>
                    <template v-else><img :src="UPLOADURL + value" alt=""></template>
                </p>
            </el-col>
        </el-row>
        <el-dialog :title="displayName" :visible.sync="addDialog" size="tiny" @close="cancelAddCategory" class="dialog">
            <el-form :model="msg" label-width="80px">
                <el-form-item label="类型">
                    <el-radio-group v-model="msg.topType">
                        <el-radio label="文字"></el-radio>
                        <el-radio label="图片"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="文字" v-if="msg.topType == '文字'">
                    <el-input type="textarea":rows="8" v-model="msg.value" auto-complete="off" :placeholder="'请输入' + displayName" :maxlength="20"></el-input>
                </el-form-item>
                <el-form-item v-else label="上传照片">
                    <el-upload class="avatar-uploader" :headers="uploadHeader" :action="BASEURL+'/upload/top'" :show-file-list="false" :on-success="handleAvatarSuccess">
                       <el-button size="small" type="primary">上传</el-button>
                   </el-upload>
                    <div>
                        <img v-if="imgUrl" :src="UPLOADURL + imgUrl" alt="" class="banner-thumb">
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAddCategory">取 消</el-button>
                <el-button type="primary" @click="set">保 存</el-button>
            </div>
        </el-dialog>
    </el-row>
</template>
<script>
import {
    getConfig,
    setConfig
} from '@/api/api'
export default {
    data() {
        return {
        	canEmpty: false,
        	displayName: '顶部文字',
        	key: 'top',
        	value: '',
            type: '文字',
            addDialog: false,
            msg: {
                value: '',
                topType: '文字'
            },
            imgUrl: '',
            uploadHeader: {
                TOKEN: sessionStorage.getItem('jwt'),
                APPID: 1
            }
        }
    },
    created() {
        getConfig(this.key).then(res => {
            console.log(res)
            try{
                this.msg = JSON.parse(res[this.key]);
                this.type = this.msg.topType;
                this.value = this.msg.value;
            }catch (e) {
                this.msg = {
                    value: '',
                    topType: '文字'
                };
                this.type = '文字';
                this.value = ''
            }
        })
    },
    methods: {
        set(){
            if(!this.canEmpty && !this.msg.value){
                this.$message({
                    type: 'error',
                    message: '请输入' + this.displayName
                })
                return;
            }
            this.type = this.msg.topType;
            var data = {
                top: JSON.stringify(this.msg)
            }
            setConfig(data).then(res => {
                console.log(res)
                this.value = this.msg.value;
                this.cancelAddCategory()
            })
        },
        addConfig: function(){
            this.addDialog = true;
            console.log(this.value,this.type)
            this.msg.value = this.value;
            if(this.type == '图片'){
                this.imgUrl =  this.msg.value;
            }
        },
        cancelAddCategory: function(){
            this.addDialog = false;
        },
        handleAvatarSuccess: function(res){
            if(res.status){
                this.$message({
                    message: '上传图片成功',
                    type: 'success'
                })
                this.type = "图片"
                this.imgUrl = res.data.webPath;
                this.msg.value = res.data.webPath;
            }else{
                this.$message({
                    message: '上传图片失败',
                    type: 'error'
                })
            }
        }
    }
}
</script>
<style scoped lang="scss">
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
    .banner-thumb{
        max-width: 100%;
    }
}
</style>